from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
import matplotlib.pyplot as plt
from sklearn.metrics import calinski_harabasz_score

x, y = make_blobs(n_samples=1000,
                  n_features=2,
                  centers=[[-1,-1], [0,0], [1,1], [2,2]],
                  cluster_std = [0.4, 0.2, 0.2, 0.2],
                  random_state=22)
plt.figure(figsize=(15,8),dpi=80)
# plt.scatter(x[:,0],x[:,1],marker="o")
# plt.show()
# 3 使用k-means进行聚类, 并使用CH方法评估
y_pred = KMeans(n_clusters=4, random_state=22).fit_predict(x)
plt.scatter(x[:, 0], x[:, 1],marker="o", c=y_pred)
plt.show()
# 4 模型评估
print(calinski_harabasz_score(x, y_pred))









